package com.youdelu.road;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.youdelu.road.adapter.ListAdapter;
import com.youdelu.road.adapter.PointListAdapter;
import com.youdelu.road.bean.Attr;
import com.youdelu.road.bean.BgAttr;
import com.youdelu.road.bean.PositionAttr;
import com.youdelu.road.utils.AppConfig;
import com.youdelu.road.utils.BroadcastManager;
import com.youdelu.road.utils.Util;

import java.util.ArrayList;
import java.util.List;

public class PointListActivity extends AppCompatActivity  implements View.OnClickListener,PointListAdapter.OnClickInterface {



    public static final String SAVE_POSITION_IMG = "save_position_img";
    private Context mContext;
    private DbUtils db ;
    private TextView add_bg ;
    private ListView list ;
    private PointListAdapter adapter;
    private List<PositionAttr> bgAttrs = new ArrayList<>();
    private int  bg_id  = 0 ;
    private TextView title ;
    private Gson gson ;
    private String bgconf ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_list);
        mContext = this ;
        db = Util.db;

        bgconf = getIntent().getStringExtra("bgconf");

        add_bg = findViewById(R.id.add_bg);
        list = findViewById(R.id.list);
        title = findViewById(R.id.title);
        title.setText("选择位置");
        adapter = new PointListAdapter(mContext,this,bgAttrs);
        list.setAdapter(adapter);
        add_bg.setOnClickListener(this);
        gson = new Gson();
        String bgid = AppConfig.getInstance(mContext).get(MainActivity.IMG_BGID);
        if(bgid!=null){
            bg_id = Integer.parseInt(bgid);
        }
        initBroad();
        reflushAdapter();
    }

    public void initBroad(){

        BroadcastManager.getInstance(mContext).addAction(SAVE_POSITION_IMG, new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String str = intent.getStringExtra("attr");
                if(str!=null){
                    Attr attr = gson.fromJson(str,Attr.class);
                    PositionAttr ptr = new PositionAttr();
                    ptr.setName(attr.getTitle());
                    ptr.setData(bgconf);
                    try {
                        db.save(ptr);
                    }catch (Exception e){

                    }
                    reflushAdapter();
                }
            }
        });
    }
    public void reflushAdapter(){
        try {
            bgAttrs = db.findAll(Selector.from(PositionAttr.class).where("bgId","=",bg_id));
            adapter.update(bgAttrs,bg_id);
        }catch (Exception e){

        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.add_bg:
                Intent intent = new Intent();
                intent.putExtra("id",0);
                intent.putExtra("bgconf",bgconf);
                intent.setClass(mContext, AddMapBgActivity.class);
                startActivity(intent);
                break;
        }

    }


    @Override
    protected void onDestroy() {
        BroadcastManager.getInstance(mContext).destroy(SAVE_POSITION_IMG);
        super.onDestroy();
    }

    @Override
    public void open(PositionAttr at) {
        Attr attr = new Attr();
        attr.setPointX(at.getX());
        attr.setPointY(at.getY());
        attr.setTitle(at.getName());
        attr.setPath(at.getData());
        BroadcastManager.getInstance(mContext).sendBroadcast(MainActivity.MOVE_POSITION,attr);
        finish();
    }

    @Override
    public void del(PositionAttr attr) {
        try{
            db.delete(attr);
        }catch (Exception e){

        }
        reflushAdapter();
    }
}
